package com.rainyun.rc.util;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;


/**雨之过滤器*/
public class RainF {

	/**obj是否在arr数组里*/
	public static <T> boolean inArray(T[] arr,T obj){
		for(T o:arr) if(obj==o||obj.equals(o))return true;
		return false;
	}

	/**数组快捷过滤(顺便直接变成list)
	 * @param arr 要过滤的数组
	 * @param filter 过滤规则 (obj)->boolean
	 * */
	public static <T> List<T> filter(T[] arr, Predicate<? super T> filter) {
		return Arrays.stream(arr).filter(filter).collect(Collectors.toList());
	}
	
	/**list快捷过滤
	 * @param list 要过滤的list
	 * @param filter 过滤规则 (obj)->boolean
	 * */
	public static <T> List<T> filter(List<T> list, Predicate<? super T> filter) {
		return list.stream().filter(filter).collect(Collectors.toList());
	}
	/**set快捷过滤
	 * @param set 要过滤的set
	 * @param filter 过滤规则 (obj)->boolean
	 * */
	public static <T> Set<T> filter(Set<T> set, Predicate<? super T> filter) {
		return set.stream().filter(filter).collect(Collectors.toSet());
	}
	/**map快捷过滤
	 * @param map 要过滤的map
	 * @param filter 过滤规则 (entry)->boolean
	 * */
	public static <K,V> Map<K,V> filter(Map<K,V> map, Predicate<? super Entry<K,V>> filter) {
		return map.entrySet().stream()
		.filter(filter)
		.collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));
	}
	
	
	
}
